package com.example.service.impl;

import com.example.mapper.LaboratoryMapper;
import com.example.model.Laboratory;
import com.example.service.LabService;
import com.example.util.DataMap;
import com.example.util.ErrorStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: tao95
 * Date: 2017/5/11
 * Time: 11:29
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class LabServiceImpl implements LabService {

    @Autowired
    private LaboratoryMapper laboratoryMapper;

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public DataMap selectLabList() {
        DataMap dataMap = new DataMap();
        List<Laboratory> labList = laboratoryMapper.selectLabList();
        dataMap.addAttribute("labList",labList);
        return dataMap;
    }

    @Override
    @Transactional
    public DataMap insertLab(Laboratory laboratory) {
        DataMap dataMap = new DataMap();
        if(laboratory.getId() == null){
            try {
                laboratoryMapper.insertLab(laboratory);
            }catch(Exception e){
                logger.error(e.getMessage());
                dataMap.put("code", ErrorStatusEnum.ADD_LABFAIL.getErrorCode());
                return dataMap;
            }
        }else{
            try{
                laboratoryMapper.editLab(laboratory);
            }catch (Exception e){
                logger.error(e.getMessage());
                dataMap.put("code", ErrorStatusEnum.EDIT_LABFAIL.getErrorCode());
                return dataMap;
            }
        }
        dataMap.put("code", ErrorStatusEnum.SUCCESS.getErrorCode());
        return dataMap;
    }

    @Override
    @Transactional
    public DataMap delLab(Integer id) {
        DataMap dataMap = new DataMap();
        try{
            laboratoryMapper.delLab(id);
        }catch (Exception e){
            logger.error(e.getMessage());
            dataMap.put("code", ErrorStatusEnum.DEL_LABFAIL.getErrorCode());
            return dataMap;
        }
        dataMap.put("code", ErrorStatusEnum.SUCCESS.getErrorCode());
        return dataMap;
    }

    @Override
    public Integer findTopLab() {
        Integer laboratoryId = null;
        try {
            laboratoryId = laboratoryMapper.findTopLab();
        }catch (Exception e){
                logger.error(e.getMessage());
        }
        return laboratoryId;
    }
}